package com.ruoyi.cases.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cases.domain.JlTroubleshootingTable;
import com.ruoyi.cases.utils.excel.ExcelExportToFileUrlComplex;
import com.ruoyi.cases.utils.excel.HexBeanUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cases.domain.JlDailyStatistics;
import com.ruoyi.cases.service.IJlDailyStatisticsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 个人日统计Controller
 * 
 * @author ruoyi
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/cases/statisticsDaily")
public class JlDailyStatisticsController extends BaseController
{
    @Autowired
    private IJlDailyStatisticsService jlDailyStatisticsService;

    /**
     * 查询个人日统计列表
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:list')")
    @GetMapping("/list")
    public TableDataInfo list(JlDailyStatistics jlDailyStatistics)
    {
        startPage();
        List<JlDailyStatistics> list = jlDailyStatisticsService.selectJlDailyStatisticsList(jlDailyStatistics);
        return getDataTable(list);
    }

    /**
     * 导出个人日统计列表
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:export')")
    @Log(title = "个人日统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JlDailyStatistics jlDailyStatistics)
    {
        List<JlDailyStatistics> list = jlDailyStatisticsService.selectJlDailyStatisticsList(jlDailyStatistics);
        ExcelUtil<JlDailyStatistics> util = new ExcelUtil<JlDailyStatistics>(JlDailyStatistics.class);
        util.exportExcel(response, list, "个人日统计数据");
    }

    /**
     * 获取个人日统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(jlDailyStatisticsService.selectJlDailyStatisticsById(id));
    }

    /**
     * 新增个人日统计
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:add')")
    @Log(title = "个人日统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JlDailyStatistics jlDailyStatistics)
    {
        return toAjax(jlDailyStatisticsService.insertJlDailyStatistics(jlDailyStatistics));
    }

    /**
     * 修改个人日统计
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:edit')")
    @Log(title = "个人日统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JlDailyStatistics jlDailyStatistics)
    {
        return toAjax(jlDailyStatisticsService.updateJlDailyStatistics(jlDailyStatistics));
    }

    /**
     * 删除个人日统计
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:remove')")
    @Log(title = "个人日统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(jlDailyStatisticsService.deleteJlDailyStatisticsByIds(ids));
    }

    /**
     * 获取个人日统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:query')")
    @GetMapping(value = "/getDetail")
    public List<JlDailyStatistics> getDetail(JlDailyStatistics jlDailyStatistics)
    {
        return jlDailyStatisticsService.getDetail(jlDailyStatistics);
    }

    /**
     * 导出病害排查列表
     */
    @PreAuthorize("@ss.hasPermi('cases:statisticsDaily:export')")
    @Log(title = "个人日统计自定义版本", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTest")
    public void exportTest(HttpServletRequest request, HttpServletResponse response, JlDailyStatistics jlDailyStatistics)
    {
        List<JlDailyStatistics> list = jlDailyStatisticsService.selectJlDailyStatisticsList(jlDailyStatistics);
        list.add(new JlDailyStatistics());
        list.add(new JlDailyStatistics());
        try{
            int xh = 1;
            Map model = new HashMap();
            String username = "";
            List<Map<String, Object>> listdata = new ArrayList<>();
            for (JlDailyStatistics val: list) {
                val.setXh(xh);
                Map<String, Object> map = HexBeanUtils.object2Map(val);
                listdata.add(map);
                xh++;
            }
            String name ="xh,statisticsDate,deptName,username,statisticsDetail,ratedCoefficient,assessorName,remark";
            model.put("excelTitleClos", name.split(",").length-1);
            model.put("excelTitle", list.get(0).getUsername()+"养护工区定额工作完成情况评价表（个人）");
            //[[行号,列号(&分隔),文字(&分隔),合并单元格(起始行,结束行,起始列,结束列)]]
            model.put("names", name.split(","));
            model.put("excelTitleFontSize", "20");
            model.put("titles1",
                    new String[]{"序号", "日期", "工区", "姓名", "工作量明细","额定系数","考核人","备注"});
            model.put("titleStarRows", 1);
            model.put("titleRows", 2);
            model.put("list", listdata);
            String columnWidth =
                    "0,15&1,30&2,20&3,20&4,100&5,25&6,25&7,25&8,25&9,50";
            model.put("columnWidth", columnWidth);
            new ExcelExportToFileUrlComplex().buildExcelDocument1(model, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
